﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKStairs.CommonUnit
{
	public class ElementGeometryManager
	{
		public static List<Solid> GetGeometryInstanceSolid(GeometryInstance geoInstance)
		{
			List<Solid> list = new List<Solid>();
			foreach (GeometryObject geometryObject in geoInstance.GetInstanceGeometry())
			{
				if (geometryObject.GetType() == typeof(Solid))
				{
					Solid solid = geometryObject as Solid;
					if (null != solid)
					{
						list.Add(solid);
					}
				}
			}
			return list;
		}

		public static List<Solid> GetElementAllSolid(Element element)
		{
			List<Solid> list = new List<Solid>();
			Options options = new Options();
			foreach (GeometryObject geometryObject in element.get_Geometry(options))
			{
				Type type = geometryObject.GetType();
				if (type == typeof(GeometryInstance))
				{
					GeometryInstance geoInstance = geometryObject as GeometryInstance;
					list.AddRange(ElementGeometryManager.GetGeometryInstanceSolid(geoInstance));
				}
				else if (type == typeof(Solid))
				{
					Solid solid = geometryObject as Solid;
					if (null != solid)
					{
						list.Add(solid);
					}
				}
			}
			return list;
		}

		public static List<Face> GetElementAllFace(Element element)
		{
			List<Face> list = new List<Face>();
			if (element == null)
			{
				return list;
			}
			List<Solid> elementAllSolid = ElementGeometryManager.GetElementAllSolid(element);
			if (elementAllSolid.Count > 0)
			{
				foreach (Solid solid in elementAllSolid)
				{
					if (null != solid)
					{
						foreach (object obj in solid.Faces)
						{
							Face face = (Face)obj;
							if (null != face)
							{
								list.Add(face);
							}
						}
					}
				}
			}
			return list;
		}

		public static List<Curve> FaceToCurveLoop(Face face)
		{
			List<Curve> list = new List<Curve>();
			foreach (object obj in face.EdgeLoops)
			{
				EdgeArray edgeArray = (EdgeArray)obj;
				new List<Curve>();
				foreach (object obj2 in edgeArray)
				{
					Curve curve = ((Edge)obj2).AsCurve();
					if (null != curve)
					{
						list.Add(curve);
					}
				}
			}
			return list;
		}

		public static XYZ CurveToVector(Curve curve)
		{
			XYZ zero = XYZ.Zero;
			XYZ endPoint = curve.GetEndPoint(0);
			return curve.GetEndPoint(1) - endPoint;
		}

		public static List<XYZ> GetCurveStartEndPoints(Curve curve)
		{
			return new List<XYZ>
			{
				curve.GetEndPoint(0),
				curve.GetEndPoint(1)
			};
		}
	}
}
